Code
```{r}
rand_ds <- data.frame(x = rnorm(1000, mean = 10, sd = 1),
y = rnorm(1000, mean = 5, sd = 2))
rand_ds
```rnorm(_) function```{r}
rand_ds <- data.frame(x = rnorm(1000, mean = 10, sd = 1),
y = rnorm(1000, mean = 5, sd = 2))
rand_ds
```plot(_) function```{r}
plot(x = rand_ds$x, y = rand_ds$y)
```ggplot2:: packageggplot2 were initially developed independently, but later harmonised with tidyverse packages```{r}
library(ggplot2)
ggplot(rand_ds, aes(x, y)) + geom_point()
```In this practical, we will use data asthmads_clean.sav from dataset folder
```{r}
library(tidyverse)
```── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
```{r}
library(haven)
asthmads_clean <- read_sav("asthmads_clean.sav") %>%
as_factor() %>%
mutate(Wt_Diff = Weight_Post - Weight_Pre)
asthmads_clean
```ggplot(_), defining a plot object that you then add layers to.```{r}
ggplot(data = asthmads_clean)
asthmads_clean %>%
ggplot(data = .)
```ggplot(_) how the information from our data will be visually represented.aes(_) function,aes(_) specify which variables to map to the x and y axes.```{r}
asthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff))
```geom_geom_point(_) function```{r}
asthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff)) +
geom_point()
``````{r}
asthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff,
colour = Gender)) +
geom_point()
``````{r}
asthmads_clean %>%
ggplot(aes(Gender)) +
geom_bar()
``````{r}
asthmads_clean %>%
ggplot(aes(x = Gender, fill = Gender)) +
geom_bar()
``````{r}
asthmads_clean %>%
ggplot(aes(Weight_Pre)) +
geom_histogram()
````stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
```{r}
asthmads_clean %>%
ggplot(aes(x = Weight_Pre)) +
geom_histogram(fill = "white", colour = "black")
````stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
```{r}
asthmads_clean %>%
ggplot(aes(x = Weight_Pre)) +
geom_histogram(fill = "white", colour = "black", binwidth = 2)
``````{r}
time_ds <- tibble(time = 1:10,
value = c(2, 3, 5, 7, 8, 9, 10, 12, 14, 15))
time_ds
``````{r}
time_ds %>%
ggplot(aes(x = time, y = value)) +
geom_line()
``````{r}
asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight")
```geom_point(_)```{r}
asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight") %>%
ggplot(aes(x = event,
y = weight)) +
geom_point() +
geom_line(aes(group = idR))
``````{r}
asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight") %>%
ggplot(aes(x = event,
y = weight,
colour = Gender)) +
geom_point() +
geom_line(aes(group = idR))
``````{r}
nhms19_adm <- read_csv("../dataset/nhms19_adm.csv")
```Rows: 16 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): State
dbl (3): Prevalence, Upper_CI, Lower_CI
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```{r}
nhms19_adm
``````{r}
#| eval: false
download.file(
url = "https://raw.githubusercontent.com/dosm-malaysia/data-open/main/datasets/geodata/administrative_1_state.geojson",
destfile = "administrative_1_state.geojson",
mode = "wb")
```sf package```{r}
library(sf)
```Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
```{r}
my_state_sf <- read_sf("administrative_1_state.geojson")
my_state_sf
``````{r}
#| eval: false
left_join(nhms19_adm, my_state_sf)
``````{r}
my_state_sf <- my_state_sf %>%
rename(State = state)
my_state_sf
full_join(nhms19_adm, my_state_sf)
```Joining with `by = join_by(State)`
```{r}
my_state_sf <- my_state_sf %>%
mutate(State = fct_recode(State,
"WP Kuala Lumpur" = "W.P. Kuala Lumpur",
"WP Putrajaya" = "W.P. Putrajaya",
"WP Labuan" = "W.P. Labuan"))
nhms19_adm_m <- full_join(nhms19_adm, my_state_sf)
```Joining with `by = join_by(State)`
```{r}
nhms19_adm_m
```geom_sf(_) function```{r}
#| eval: false
nhms19_adm_m %>%
ggplot(aes(fill = Prevalence)) +
geom_sf()
``````{r}
nhms19_adm_sf <- st_as_sf(nhms19_adm_m)
nhms19_adm_sf
```geom_sf(_) function```{r}
nhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf()
``````{r}
nhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf() +
scale_fill_gradient(low = "green", high = "red")
``````{r}
nhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf() +
scale_fill_gradient(low = "green", high = "red") +
theme_bw() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank())
```